<?php
//模型
class ModuleModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'module';
		
		
	}
	
	function get_list($filter=array()){
		
		$ls_arr = $this->get_module_dirs();
		$list = $this->getAll();
		
		
		$arr = array();
		foreach($ls_arr as $key=>$val){
			$val['is_install'] = 0;
			$val['is_enable'] = 0;
			foreach($list as $row){
				if($val['module_name']==$row['module_name']){
					$val['is_install'] = 1;
					$val['is_enable'] = intval($row['is_enable']);
					break;
				}
			}
			
			$is_add = true;
			foreach($filter as $k=>$v){
				if(isset($val[$k])){
					if($val[$k]!==$v){
						$is_add = false;
					}
				}
			}
			
			if($is_add){
				$arr[] = $val;
			}
		}
		
		return $arr;
	}
	
	function get_module_dirs(){
		
		$dir = MODULEPATH;
		
		$files = array();
		
		if(@$handle = opendir($dir)) { //注意这里要加一个@，不然会有warning错误提示：）
			while(($file = readdir($handle)) !== false) {
				if($file != ".." && $file != ".") { //排除根目录；
					if(is_dir($dir."/".$file)) { //如果是子文件夹，就进行递归
						$mpath = $dir."/".$file.'/info.php';
						if(!file_exists($mpath)){
							continue;
						}
						$ls_arr = include($mpath);
						$ls_arr['path'] = $dir."/".$file;
						$ls_arr['module_name'] = $file;
						
						$files[] = $ls_arr;
					}
				}
			}
			closedir($handle);
		}
		
		return $files;
	}
	
	function get_module($name){
		$arr = $this->get_module_dirs();
		foreach($arr as $val){
			if($val['module_name']==$name){
				return $val;
			}
		}
		return false;
	}
	
	function field_get_info($val,$field='id'){
		
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where `{$field}`='{$val}' limit 1";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
		
	}
}

